package suanfa;

import java.util.HashSet;
import java.util.Set;

/**
 *  获取最大字符串长度，时间复杂度是O(n)
 */
public class StrLength
{

    public int getMaxLength(String string)
    {
        int n = string.length();
        int res = 0;
        int end = 0, start = 0;
        Set<Character> set = new HashSet<>();
        while (start < n && end < n)
        {
            if (set.contains(string.charAt(end)))
            {
                //移除开始的元素
                set.remove(string.charAt(start++));
            }else
            {
                set.add(string.charAt(end++));
                res = Math.max(res, end - start);
            }
        }
        return res;
    }

    public static void main(String[] args)
    {
        StrLength strLength = new StrLength();
        int length = strLength.getMaxLength("ccsdcdsvcfdfs");
        System.out.println(length);
    }
}
